<template>
  <div class="cashier-page-layout">
    <div class="container">
      <div class="title">
        <div class="name">{{ orderInfo.name }}</div>
        <div class="desc">{{ orderInfo.desc }}</div>
        <div class="tags">
          <van-tag
            v-for="(item, index) in orderInfo.tags"
            :key="index"
            plain
            color="#4586FF"
          >{{ item }}</van-tag>
        </div>
      </div>

      <div class="amount">
        <div class="label">订单总价：</div>
        <div class="value">{{ `¥ ${ orderInfo.amount }` }}</div>
      </div>

      <div class="coupon" @click="$refs.couponPopup.open()">
        <div class="label">
          <img src alt />
          <span>优惠券</span>
        </div>
        <div class="value">
          <div v-show="coupon.id">
            {{ `（${coupon.name}）` }}
            <span>{{ `-${coupon.price}元` }}</span>
          </div>
          <van-icon name="arrow" color="#999999" />
        </div>
      </div>

      <div class="point">
        <div class="heading">
          <div class="label">
            <img src alt />
            <span>使用积分抵扣</span>
          </div>
          <div class="value">
            <span>{{ `-3.00元` }}</span>
          </div>
        </div>

        <div class="desc">可用积分：130.00，本次消耗积分30抵扣3元</div>
        <span class="remark">注：本订单最多可抵扣 5元（扣减积分50个）</span>
      </div>

      <div class="result">
        <div class="label">需付款</div>
        <div class="value">{{ `${result}元` }}</div>
      </div>

      <div class="count">
        <span class="label">支付剩余时间</span>
        <van-count-down :time="15 * 60 * 60 * 1000">
          <template v-slot="timeData">
            <span class="block">{{ timeData.hours }}</span>
            <span class="colon">:</span>
            <span class="block">{{ timeData.minutes }}</span>
            <span class="colon">:</span>
            <span class="block">{{ timeData.seconds }}</span>
          </template>
        </van-count-down>
      </div>
    </div>

    <payment-method @select="onSelectPayment" :style="{ marginTop: '5px' }" />

    <van-button
      size="large"
      :loading="state.payBtn"
      :disabled="state.payBtn"
      color="#4586FF"
      class="submit-button"
      @click="onPay"
    >立即支付</van-button>
    <div class="submit-placeholder"></div>

    <coupon-popup ref="couponPopup" @select="onSelectCoupon" />
  </div>
</template>

<script>
import Vue from 'vue'
import { Tag, Icon, CountDown, Button } from 'vant'
import { messages, toast } from '@/utils/messages'
import CouponPopup from './CouponPopup'
import PaymentMethod from './PaymentMethod'

Vue.use(Tag)
  .use(Icon)
  .use(CountDown)
  .use(Button)

export default {
  components: {
    CouponPopup,
    PaymentMethod
  },

  data() {
    return {
      orderInfo: {
        name: '数据分析行业全景概览',
        desc: '商品描述信息商品描述信息最多20个字',
        tags: ['专业培训', '读书补贴'],
        amount: 19.9,
        createTime: '1595060388'
      },
      point: 3,
      payment: '',
      coupon: {},
      state: {
        payBtn: false
      }
    }
  },

  computed: {
    result() {
      const {
        point,
        coupon: { price = 0 },
        orderInfo: { amount }
      } = this
      const result = Number(amount) - Number(price) - Number(point)
      return result > 0 ? result.toFixed(2) : Number(0).toFixed(2)
    }
  },

  methods: {
    validate() {
      return new Promise((resolve, reject) => {
        const label = '支付方式'
        const { payment } = this
        payment ? resolve() : reject(messages.REQUIRED_SELECT(label))
      })
    },

    onSelectCoupon(e) {
      this.coupon = e
    },

    onSelectPayment(e) {
      this.payment = e
    },

    onPay() {
      this.validate()
        .then(() => {
          this.state.payBtn = true
          this.$router.push({ name: 'cashier-result', params: { id: '1' } })
        })
        .catch(err => {
          toast(err)
        })
    }
  }
}
</script>

<style lang="less" scoped>
.cashier-page-layout {
  border-top: 0.5px solid #ebebeb;

  .container {
    background-color: #ffffff;
    padding: 15px 17px 0 17px;

    .title {
      text-align: center;

      .name {
        font-size: 18px;
        color: #222222;
      }

      .desc {
        font-size: 13px;
        color: #bbbbbb;
        margin-top: 1px;
      }

      .tags {
        margin-top: 5.5px;

        > * {
          margin-right: 5.5px;
          background-color: rgba(69, 134, 255, 0.1);

          &:last-child {
            margin: 0;
          }
        }
      }
    }

    .amount {
      margin-top: 16px;
      padding-bottom: 14px;
      display: flex;
      align-items: center;
      justify-content: space-between;

      .label {
        font-size: 13px;
        color: #999999;
        font-weight: 500;
      }

      .value {
        font-size: 13px;
        color: #222222;
        font-weight: 500;
      }
    }

    .coupon {
      padding: 17px 0 14.5px 0;
      border-top: 0.5px solid #ebebeb;
      display: flex;
      align-items: center;
      justify-content: space-between;

      .label {
        font-size: 16px;
        color: #666666;
        display: flex;
        align-items: center;

        img {
          width: 25px;
          height: 19px;
          margin-right: 10px;
        }
      }

      .value {
        display: flex;
        align-items: center;
        font-size: 14px;
        color: #999999;

        span {
          color: #ff6669;
        }
      }
    }

    .point {
      padding: 17px 0 14.5px 0;
      border-top: 0.5px solid #ebebeb;

      .heading {
        display: flex;
        align-items: center;
        justify-content: space-between;

        .label {
          font-size: 16px;
          color: #666666;
          display: flex;
          align-items: center;

          img {
            width: 25px;
            height: 19px;
            margin-right: 10px;
          }
        }

        .value {
          display: flex;
          align-items: center;
          font-size: 14px;
          color: #999999;

          span {
            color: #ff6669;
          }
        }
      }

      .desc {
        margin-top: 11px;
        font-size: 12px;
        color: #999999;
        margin-left: 34.5px;
        margin-bottom: 4px;
      }

      .remark {
        background-color: rgba(255, 102, 105, 0.1);
        color: #ff6669;
        font-size: 12px;
        padding: 2.5px 7px;
        border-radius: 1.5px;
        margin-left: 34.5px;
      }
    }

    .result {
      border-top: 0.5px solid #ebebeb;
      padding: 8px 0;
      display: flex;
      align-items: center;
      justify-content: space-between;

      .label {
        font-size: 14px;
        font-weight: 500;
        color: #222222;
      }

      .value {
        font-size: 21px;
        line-height: 29.5px;
        font-weight: 500;
        color: #ff6669;
      }
    }

    .count {
      border-top: 0.5px solid #ebebeb;
      padding: 14px 0 12.5px 0;
      display: flex;
      justify-content: flex-end;
      align-items: center;

      .label {
        color: #bbbbbb;
        font-size: 12px;
        margin-right: 11px;
      }

      .block {
        display: inline-block;
        text-align: center;
        width: 20px;
        height: 19px;
        font-size: 12px;
        background-color: #ffeff0;
        color: #ff6669;
        border-radius: 1.5px;
      }

      .colon {
        margin: 0 3px;
        color: #ff6669;
      }
    }
  }

  .submit-button {
    width: calc(100vw - 34px);
    margin: 0 17px;
    border-radius: 5px;
    font-size: 18px;
    position: fixed;
    bottom: 18.5px;
  }

  .submit-placeholder {
    height: 87px;
  }
}
</style>